package com.ylbh.modules.goods.controller;

import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.pojo.activity.dto.GoodsListDTO;
import com.ylbh.common.pojo.goods.dto.SubGoodsClassDTO;
import com.ylbh.common.pojo.goods.vo.GoodsTypeVO;
import com.ylbh.common.pojo.goods.vo.SpecIficationVO;
import com.ylbh.common.pojo.goodsclass.vo.ShoppingGoodsClassSpecVO;
import com.ylbh.common.utils.*;
import com.ylbh.modules.goods.service.GoodsClassService;
import com.ylbh.modules.sys.controller.AbstractController;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * @author mgh
 */
@RestController
@RequestMapping("goods/class")
public class ShoppingClassController extends AbstractController {
    @Autowired
    private GoodsClassService goodsClassService;

    @ApiOperation("商品分类信息列表")
    @RequestMapping("queryGoodsClassList")
    @RequiresPermissions("goods:class:queryGoodsClassList")
    public CommonResult<PageUtils> queryGoodsClassList(PageDTO pageDTO){
        PageUtils list = goodsClassService.queryGoodsClassList(pageDTO);
        return Ref.ok(list);
    }

    @ApiOperation("根据类型id查询规格列表")
    @RequestMapping("querySpecsListByTypeId")
    @RequiresPermissions("goods:class:queryGoodsClassList")
    public CommonResult<List<SpecIficationVO>> querySpecsListByTypeId(@RequestBody Integer typeId){
        List<SpecIficationVO> list = goodsClassService.querySpecsListByTypeId(typeId);
        return Ref.ok(list);
    }

    @ApiOperation("获取商品分类的子分类")
    @RequestMapping("queryGoodsSubClassById/{id}")
    @RequiresPermissions("goods:class:queryGoodsClassList")
    public CommonResult queryGoodsSubClassById(@PathVariable Integer id){
        List<ShoppingGoodsClassSpecVO> list = goodsClassService.queryGoodsSubClassById(id);
        return Ref.ok(list);
    }

    @ApiOperation("查询这个分类下没有选择的剩余规格")
    @RequestMapping("querySpecsList")
    public CommonResult<List<SpecIficationVO>> querySpecsList(@RequestBody Integer[] id){
        List<SpecIficationVO> specIficationVOS = goodsClassService.querySpecsList(id);
        return Ref.ok(specIficationVOS);
    }

    /**
     * 获取商品分类列表
     * @return
     */
    @ApiOperation("获取商品分类列表")
    @RequestMapping("/queryGoodsTypeList")
    public CommonResult<List<GoodsTypeVO>> queryGoodsTypeList(){
        List<GoodsTypeVO> goodsTypeVOS = goodsClassService.queryGoodsTypeList();

        return Ref.ok(goodsTypeVOS);
    }

    @SysLog("添加商品分类")
    @ApiOperation("添加商品分类")
    @RequestMapping("add")
    @RequiresPermissions("goods:class:add")
    public CommonResult add(@RequestBody SubGoodsClassDTO subGoodsClassDTO){
        goodsClassService.add(subGoodsClassDTO);
        return Ref.ok();
    }

    @SysLog("修改商品分类")
    @ApiOperation("修改商品分类")
    @RequestMapping("modifyById")
    @RequiresPermissions("goods:class:modify")
    public CommonResult modifyById(@RequestBody SubGoodsClassDTO subGoodsClassDTO){
        goodsClassService.modifyById(subGoodsClassDTO);
        return Ref.ok();
    }

    @SysLog("添加二级分类")
    @ApiOperation("添加二级分类")
    @PostMapping("addSecondLevelClass")
    @RequiresPermissions("goods:class:add")
    public CommonResult addSecondLevelClass(@RequestBody SubGoodsClassDTO subGoodsClassDTO) {
        goodsClassService.addSecondLevelClass(subGoodsClassDTO);
        return Ref.ok();
    }

    @SysLog("删除商品分类")
    @ApiOperation("删除商品分类")
    @RequestMapping("remove")
    @RequiresPermissions("goods:class:remove")
    public CommonResult remove(@RequestBody Integer[] ids){
        SubGoodsClassDTO subGoodsClassDTO = new SubGoodsClassDTO();
        subGoodsClassDTO.setDeleteStatus(1);
        for (Integer id:ids) {
            subGoodsClassDTO.setId(id);
            goodsClassService.modifyById(subGoodsClassDTO);
        }
        return Ref.ok();
    }
    @SysLog("解除规格与分类的绑定关系")
    @ApiOperation("解除规格与分类的绑定关系")
    @RequestMapping("specsRemove/{typeId}")
    @RequiresPermissions("goods:class:remove")
    public CommonResult specsRemove(@PathVariable Integer typeId,@RequestBody Integer specId){
            goodsClassService.removeSpecByTypeId(typeId,specId);
        return Ref.ok();
    }

    @SysLog("对当前分类添加规格")
    @ApiOperation("对当前分类添加规格")
    @RequestMapping("addClassSpecs/{typeId}")
    public CommonResult addClassSpecs(@PathVariable Integer typeId,@RequestBody Integer specId){
        goodsClassService.addClassSpec(typeId,specId);
        return Ref.ok();
    }

    @ApiOperation("商品分类属性上传图标")
    @RequestMapping("uploadGoodsClassSpecImg")
    public CommonResult uploadGoodsClassSpecImg(MultipartFile file) {
        OSSClientUtil util = new OSSClientUtil();
        String icon = util.uploadImg2Oss(file, "icon");
        if ("fail".equals(icon))
            return Ref.error("上传失败");
        else
            return Ref.ok("consumer/icon/" + icon);
    }
}
